-- Init Bandit
local baji = SPAWN:New("Baji") -- 4 generation fighter
    :InitLimit(1, 1)
    :InitRandomizeTemplatePrefixes("Baji Template #")
    :OnSpawnGroup(function(group)
        local PatrolZone = ZONE:New("Baji")
        local AICapZone = AI_CAP_ZONE:New(PatrolZone, 4572, 9144, 500, 600, "RADIO")
        local EngageZoneGroup = GROUP:FindByName("Baji Engage Zone")
        local EngageZone = ZONE_POLYGON:New("Baji Engage Zone", EngageZoneGroup)
        AICapZone:SetControllable(group)
        AICapZone:SetEngageZone(EngageZone)
        AICapZone:__Start(1)
    end
    )
    :SpawnScheduled(4, 0.3)
    :SpawnScheduleStart()

local baji_WWII = SPAWN:New("Baji WWII")            -- WWII fighter
local baton_flag = USERFLAG:New("baton")            -- baton spawn flag
local baji_number = 1                               -- 4 generation fighter number
local baji_type = true                              -- true for missle, false for gun
local baji_helo = SPAWN:New("Baji Helo")            -- red helo
local baji_coldwar = SPAWN:New("Baji ColdWar")      -- coldwar bandit
local bluetank_WWII = SPAWN:New("BLUE Tank WWII-1") -- WWII blue tank
local redtank_WWII = SPAWN:New("RED Tank WWII-1")   -- WWII red tank
local redbase_WWII = SPAWN:New("RED Base WWII-1")   -- WWII red base


-- 清理靶机函数
function BajiClean()
    local bajigroups = SET_GROUP:New():FilterPrefixes("Baji#"):FilterStart()
    bajigroups:ForEachGroupAlive(function(cleanupgroup)
        local tempgroup = Group.getByName(cleanupgroup:GetName())
        if tempgroup ~= nil then
            tempgroup:destroy()
        end
    end
    )
end

-- BLUE AEW
local aew = SPAWN:New("BLUE AEW"):InitRepeat()
aew:Spawn()

-- GROUND TGTs
local gnd_tgt_cars_01 = SPAWN:New("AG-1")         -- vehicle move
gnd_tgt_cars_01:Spawn()
local gnd_tgt_base_01 = SPAWN:New("BASE")         -- base stop
gnd_tgt_base_01:Spawn()
local gnd_tgt_cars_02 = SPAWN:New("RED GNDTGT-1") -- vehicle stop
gnd_tgt_cars_02:Spawn()

-- Refueling
local S3B = SPAWN:New("Fuel-1"):InitRepeat()   -- soft
S3B:Spawn()
local KC135 = SPAWN:New("Fuel-2"):InitRepeat() -- hard
KC135:Spawn()

-- MENUs
local train_menu = MENU_MISSION:New("- 训练菜单", nil)

local airtgt_menu = MENU_MISSION:New("- 空战目标", train_menu)

-- 刷新靶机
MENU_MISSION_COMMAND:New("-- 刷新空战训练靶机", airtgt_menu,
    function()
        local report_msg = "====================\n正在生成的靶机数量: " ..
            tostring(baji_number) .. "\n 靶机种类: "
        local PlanePrefixes = nil
        if baji_type == true then
            PlanePrefixes = "Baji Template #"
            report_msg = report_msg .. "导弹\n===================="
        elseif baji_type == false then
            PlanePrefixes = "Baji Template Gun #"
            report_msg = report_msg .. "机炮\n===================="
        else
            MessageToAll("设置为空！联系管理员", 10, "DEBUG")
        end

        local tempsets = SET_GROUP:New():FilterPrefixes("Baji#"):FilterStart()
        tempsets:ForEachGroupAlive(function(group)
            group:Destroy()
        end)

        if PlanePrefixes then
            local tempplane = SPAWN:New("Baji") -- 4 generation fighter
                :InitLimit(baji_number, baji_number)
                :InitRandomizeTemplatePrefixes(PlanePrefixes)
                :OnSpawnGroup(function(group)
                    local PatrolZone = ZONE:New("Baji")
                    local AICapZone = AI_CAP_ZONE:New(PatrolZone, 4572, 9144, 500, 600, "RADIO")
                    local EngageZoneGroup = GROUP:FindByName("Baji Engage Zone")
                    local EngageZone = ZONE_POLYGON:New("Baji Engage Zone", EngageZoneGroup)
                    AICapZone:SetControllable(group)
                    AICapZone:SetEngageZone(EngageZone)
                    AICapZone:__Start(1)
                end
                )
                :SpawnScheduled(4, 0.3)
                :SpawnScheduleStart()
            MessageToAll("刷新完成", 10, "DEBUG")
        else
            MessageToAll("baji或者planeprefixes为空! 联系管理员。", 10, "DEBUG")
        end
    end)

-- 清除靶机
MENU_MISSION_COMMAND:New("-- 清除空战训练靶机", airtgt_menu,
    function()
        local bajigroups = SET_GROUP:New():FilterPrefixes("Baji"):FilterStart()
        bajigroups:ForEachGroupAlive(function(group)
            if group then
                group:Destroy()
            else
                MessageToAll("未找到群组", 10, "DEBUG")
            end
        end)
    end)


local airtgtinit_menu = MENU_MISSION:New("- 空战训练设置", airtgt_menu)
-- 设置靶机数量
local setnumber_airtgt = MENU_MISSION:New("-- 设置靶机数量", airtgtinit_menu)

-- 1
MENU_MISSION_COMMAND:New("-- 1", setnumber_airtgt,
    function()
        baji_number = 1
        MessageToAll("====================\n靶机数量已经设置为: " ..
            tostring(baji_number) .. "\n" ..
            "====================", 10, "训练项目设置")
    end)

-- 2
MENU_MISSION_COMMAND:New("-- 2", setnumber_airtgt,
    function()
        baji_number = 2
        MessageToAll("====================\n靶机数量已经设置为: " ..
            tostring(baji_number) .. "\n" ..
            "====================", 10, "训练项目设置")
    end)

-- 3
MENU_MISSION_COMMAND:New("-- 3", setnumber_airtgt,
    function()
        baji_number = 3
        MessageToAll("====================\n靶机数量已经设置为: " ..
            tostring(baji_number) .. "\n" ..
            "====================", 10, "训练项目设置")
    end)
-- 4
MENU_MISSION_COMMAND:New("-- 4", setnumber_airtgt,
    function()
        baji_number = 4
        MessageToAll("====================\n靶机数量已经设置为: " ..
            tostring(baji_number) .. "\n" ..
            "====================", 10, "训练项目设置")
    end)
-- 加1
MENU_MISSION_COMMAND:New("-- +1", setnumber_airtgt,
    function()
        if baji_number then
            baji_number = baji_number + 1
            MessageToAll("====================\n靶机数量已经设置为: " ..
                tostring(baji_number) .. "\n" ..
                "====================", 10, "训练项目设置")
        else
            MessageToAll("靶机数量未设置初始值", 10, "DEBUG")
        end
    end)

-- 减1
MENU_MISSION_COMMAND:New("-- -1", setnumber_airtgt,
    function()
        if baji_number then
            baji_number = baji_number - 1
            MessageToAll("====================\n靶机数量已经设置为: " ..
                tostring(baji_number) .. "\n" ..
                "====================", 10, "训练项目设置")
        else
            MessageToAll("靶机数量未设置初始值", 10, "DEBUG")
        end
    end)

-- 设置靶机武器
local settype_airtgt = MENU_MISSION:New("-- 设置靶机武器", airtgtinit_menu)

MENU_MISSION_COMMAND:New("-- 导弹", settype_airtgt,
    function()
        baji_type = true
        MessageToAll("====================\n靶机武器已经设置为: 导弹\n" ..
            "====================", 10, "训练项目设置")
    end)
MENU_MISSION_COMMAND:New("-- 机炮", settype_airtgt,
    function()
        baji_type = false
        MessageToAll("====================\n靶机武器已经设置为: 机炮\n" ..
            "====================", 10, "训练项目设置")
    end)

-- 刷新冷战机
MENU_MISSION_COMMAND:New("-- 刷新冷战机", airtgt_menu,
    function()
        baji_coldwar:Spawn()
        MessageToAll("====================\n已刷新冷战机\n地点:苏呼米以东\n" ..
            "====================", 10, "训练项目刷新")
    end)

-- 刷新二战机
MENU_MISSION_COMMAND:New("-- 刷新二战机", airtgt_menu,
    function()
        baji_WWII:Spawn()
        MessageToAll("====================\n已刷新二战机\n地点:对地区以南\n" ..
            "====================", 10, "训练项目刷新")
    end)

-- 刷新直升机
MENU_MISSION_COMMAND:New("-- 刷新直升机", airtgt_menu,
    function()
        baji_helo:Spawn()
        MessageToAll("====================\n已刷新直升机\n地点:对地区内\n" ..
            "====================", 10, "训练项目刷新")
    end)

-- 地面目标
local gnd_tgt = MENU_MISSION:New("- 地面目标", train_menu)
-- -地面车队
MENU_MISSION_COMMAND:New("-- 刷新地面移动车队", gnd_tgt,
    function()
        MessageToAll("====================\n已刷新地面移动车队\n地点:对地区内\n" ..
            "====================", 10, "训练项目刷新")
        gnd_tgt_cars_01:Spawn()
    end)
-- -基地
MENU_MISSION_COMMAND:New("-- 刷新基地", gnd_tgt,
    function()
        gnd_tgt_base_01:Spawn()
        MessageToAll("====================\n已刷新基地\n地点:对地区内\n" ..
            "====================", 10, "训练项目刷新")
    end)

-- -固定车队
MENU_MISSION_COMMAND:New("-- 刷新地面固定车队", gnd_tgt,
    function()
        gnd_tgt_cars_02:Spawn()
        MessageToAll("====================\n已刷新地面固定车队\n地点:LM08方格野战机场\n" ..
            "====================", 10, "训练项目刷新")
    end)

-- -巴统战区
local baton_menu = MENU_MISSION:New("- 巴统联合武装战区", gnd_tgt)
-- --刷新红方
MENU_MISSION_COMMAND:New("-- 刷新红方", baton_menu,
    function()
        baton_flag:Set(1)
        MessageToAll("====================\n已刷新巴统战区\n" ..
            "====================", 10, "训练项目刷新")
    end)
-- --刷新蓝方
MENU_MISSION_COMMAND:New("-- 刷新蓝方", baton_menu,
    function()
        baton_flag:Set(2)
        MessageToAll("====================\n已刷新巴统战区蓝方\n" ..
            "====================", 10, "训练项目刷新")
    end)

-- 二战丛林肉搏
MENU_MISSION_COMMAND:New("-- 刷新二战战区", gnd_tgt, function()
    bluetank_WWII:Spawn()
    redtank_WWII:Spawn()
    redbase_WWII:Spawn()
    MessageToAll("====================\n已刷新二战战区\n" ..
        "====================", 10, "训练项目刷新")
end)

-- 空中加油重新部署
local refueling = MENU_MISSION:New("- 空中加油", nil)
-- -软管
MENU_MISSION_COMMAND:New("-- 重新刷新软管", refueling, function()
    S3B:ReSpawn()
    MessageToAll("====================\n已刷新软管加油机\n" ..
        "====================", 10, "训练项目刷新")
end)
-- -硬管
MENU_MISSION_COMMAND:New("-- 重新刷新硬管", refueling, function()
    KC135:ReSpawn()
    MessageToAll("====================\n已刷新硬管加油机\n" ..
        "====================", 10, "训练项目刷新")
end)

-- 预警机
MENU_MISSION_COMMAND:New("-- 预警机重新部署", nil, function()
    aew:ReSpawn()
    MessageToAll("====================\n已刷新空中预警机\n" ..
        "====================", 10, "训练项目刷新")
end)
